package cn.hwadee.muying.enums;

import cn.hwadee.muying.entity.Mail;
import org.apache.log4j.Logger;

/**
 * Created by Mankind on 2018/8/6.
 */
public enum MailSetting {
    _163("163.com","pop.163.com","995",true,"pop3","smtp.163.com","465",true,"smtp"),//ssl为465/994
    _2980("2980.com","pop3.2980.com","110",false,"pop3","smtp.2980.com","25",false,"smtp"),
    aliyun("aliyun.com","pop3.aliyun.com","995",true,"pop3","smtp.aliyun.com","465",true,"smtp"),//ssl为465
    _21cn("21cn.com","pop.21cn.com","995",true,"pop3","smtp.21cn.com","465",true,"smtp"),
    outlook("outlook.com","pop-mail.outlook.com","995",true,"pop3","smtp-mail.outlook.com","587",true,"smtp"),//startssl
    sina_cn("sina.cn","pop.sina.cn","995",true,"pop3","smtp.sina.cn","465",true,"smtp"),
    sina_com("sina.com","pop.sina.com","995",true,"pop3","smtp.sina.com","465",true,"smtp"),
    _126("126.com","pop.126.com","995",true,"pop3","smtp.126.com","465",true,"smtp"),
    yahoo("yahoo.com","pop.mail.yahoo.com","995",true,"pop3","smtp.mail.yahoo.com","465",true,"smtp"),
    qq("qq.com","pop.qq.com","995",true,"pop3","smtp.qq.com","465",true,"smtp"),
    sohu("sohu.com","pop3.sohu.com","995",true,"pop3","smtp.sohu.com","465",true,"smtp"),
    gmail("gmail.com","imap.gmail.com","993",true,"imap","smtp.gmail.com","587",true,"smtp"),
    vip163("vip.163.com","pop.vip.163.com","995",true,"pop3","smtp.vip.163.com","465",true,"smtp"),
    tom("tom.com","pop.tom.com","110",false,"pop3","smtp.tom.com","25",false,"smtp"),
    ;
    private static Logger log = Logger.getLogger(MailSetting.class);

    MailSetting(String mailType, String host, String port, boolean isSSL, String protocol,
                String smtpHost, String smtpPort, boolean smtpIsSSL, String smtpProtocol) {
        this.mailType = mailType;
        this.host = host;
        this.port = port;
        this.isSSL = isSSL;
        this.protocol = protocol;
        this.smtpHost = smtpHost;
        this.smtpPort = smtpPort;
        this.smtpIsSSL = smtpIsSSL;
        this.smtpProtocol = smtpProtocol;
    }

    private String mailType;
    private String host;
    private String port;
    private boolean isSSL;
    private String protocol;
    private String smtpHost;
    private String smtpPort;
    private boolean smtpIsSSL;
    private String smtpProtocol;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public boolean isSSL() {
        return isSSL;
    }

    public void setSSL(boolean SSL) {
        isSSL = SSL;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }



    public String getMailType() {
        return mailType;
    }

    public void setMailType(String mailType) {
        this.mailType = mailType;
    }
    public String getSmtpHost() {
        return smtpHost;
    }

    public void setSmtpHost(String smtpHost) {
        this.smtpHost = smtpHost;
    }

    public String getSmtpPort() {
        return smtpPort;
    }

    public void setSmtpPort(String smtpPort) {
        this.smtpPort = smtpPort;
    }

    public boolean isSmtpIsSSL() {
        return smtpIsSSL;
    }

    public void setSmtpIsSSL(boolean smtpIsSSL) {
        this.smtpIsSSL = smtpIsSSL;
    }
    public String getSmtpProtocol() {
        return smtpProtocol;
    }

    public void setSmtpProtocol(String smtpProtocol) {
        this.smtpProtocol = smtpProtocol;
    }

    public static void setMailSetting(Mail mail){
        try {
            String mailType=mail.getType();
            log.info(mail.getEmail()+"邮箱设置:类型为:"+mailType);
            //根据邮箱类型选择邮箱配置
            MailSetting[] mailSettings=MailSetting.values();
            for(MailSetting mailSetting:mailSettings){
                if(mailSetting.getMailType().equalsIgnoreCase(mailType)){
                    mail.setRecMailSetting(mailSetting);
                    log.info(mail.getEmail()+"邮箱设置成功:setting:"+mail.getRecMailSetting());
                    break;
                }
            }
        }catch (Exception e){
            log.error("设置异常:",e);
        }

    }

    public static void setMailSettingBySmtp(Mail mail){
        String mailType=mail.getSendMail().substring(mail.getSendMail().indexOf("@")+1);
        //根据邮箱类型选择邮箱配置
        MailSetting[] mailSettings=MailSetting.values();
        for(MailSetting mailSetting:mailSettings){
            if(mailSetting.getMailType().equalsIgnoreCase(mailType)){
                mail.setSendMailSetting(mailSetting);
                break;
            }
        }
    }

}
